from PIL import Image


def combined_spatial_filter_rgb(image_path, output_path, mean_kernel_size, median_kernel_size):
    """
    对输入的RGB图像进行组合空域滤波处理（先均值滤波后中值滤波）。

    参数:
    image_path (str): 输入图像的文件路径。
    output_path (str): 输出滤波后图像的文件路径。
    mean_kernel_size (int): 均值滤波的滤波核大小，通常为奇数，如3、5等，表示滤波核是mean_kernel_size * mean_kernel_size的正方形。
    median_kernel_size (int): 中值滤波的滤波核大小，通常为奇数，如3、5等，表示滤波核是median_kernel_size * median_kernel_size的正方形。
    """
    # 1. 先进行均值滤波
    def mean_filter(image, kernel_size):
        """
        对输入图像进行均值滤波处理。
        """
        width, height = image.size
        radius = kernel_size // 2
        new_image = Image.new(image.mode, (width, height))
        for y in range(radius, height - radius):
            for x in range(radius, width - radius):
                sum_red = 0
                sum_green = 0
                sum_blue = 0
                for ky in range(-radius, radius + 1):
                    for kx in range(-radius, radius + 1):
                        pixel = image.getpixel((x + kx, y + ky))
                        red, green, blue = pixel
                        sum_red += red
                        sum_green += green
                        sum_blue += blue
                mean_red = sum_red // (kernel_size * kernel_size)
                mean_green = sum_green // (kernel_size * kernel_size)
                mean_blue = sum_blue // (kernel_size * kernel_size)
                new_image.putpixel((x, y), (mean_red, mean_green, mean_blue))
        return new_image

    mean_filtered_image = mean_filter(Image.open(image_path), mean_kernel_size)

    # 2. 再进行中值滤波
    def median_filter(image, kernel_size):
        """
        对输入图像进行中值滤波处理。
        """
        width, height = image.size
        radius = kernel_size // 2
        new_image = Image.new(image.mode, (width, height))
        for y in range(radius, height - radius):
            for x in range(radius, width - radius):
                red_pixel_values = []
                green_pixel_values = []
                blue_pixel_values = []
                for ky in range(-radius, radius + 1):
                    for kx in range(-radius, radius + 1):
                        pixel = image.getpixel((x + kx, y + ky))
                        red, green, blue = pixel
                        red_pixel_values.append(red)
                        green_pixel_values.append(green)
                        blue_pixel_values.append(blue)
                sorted_red_pixel_values = sorted(red_pixel_values)
                sorted_green_pixel_values = sorted(green_pixel_values)
                sorted_blue_pixel_values = sorted(blue_pixel_values)
                median_red = sorted_red_pixel_values[len(sorted_red_pixel_values) // 2]
                median_green = sorted_green_pixel_values[len(sorted_green_pixel_values) // 2]
                median_blue = sorted_blue_pixel_values[len(sorted_blue_pixel_values) // 2]
                new_image.putpixel((x, y), (median_red, median_green, median_blue))
        return new_image

    median_filtered_image = median_filter(mean_filtered_image, median_kernel_size)

    # 保存最终滤波后的图像
    median_filtered_image.save(output_path)
    return median_filtered_image

image_path = "D:/cangku/computer-image-project-design/test.jpg"
output_path = "D:/cangku/computer-image-project-design/test1.jpg"
mean_kernel_size = 3
median_kernel_size = 3
combined_spatial_filter_rgb(image_path, output_path, mean_kernel_size, median_kernel_size)